import React, {memo, useCallback} from 'react';
import {
  View,
  Text,
  StyleSheet,
  NativeModules,
  TouchableHighlight,
  Dimensions,
} from 'react-native';
import {useSelector, useDispatch} from 'react-redux';
import PatIcon from '../base/PatIcon';
import {useNavigation} from '@react-navigation/native';
import Colors from '../../utils/Colors';
import {useTranslation} from 'react-i18next';
const HEIGHT = NativeModules.StatusBarManager.HEIGHT;
const windowWidth = Dimensions.get('window').width;

const HomeTop = () => {
  const {t} = useTranslation();
  const isLogin = useSelector(state => state.user.isLogin);
  const navigation = useNavigation();
  const dispatch = useDispatch();
  const goCart = useCallback(() => {
    console.log('去购物车', navigation);
    // navigation.navigate('about');
  }, [navigation]);
  const goSigned = useCallback(() => {
    if (isLogin) {
      console.log('去签到');
      // navigation.navigate('signed-root');
    } else {
      dispatch({type: 'user/setShowLoginRegister', payload: true});
    }
    console.log('去搜索页', navigation);
    // navigation.navigate('about');
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [navigation, isLogin]);
  const count = 8;
  return (
    <View style={[styles.homeTop]}>
      <View style={styles.bell}>
        <PatIcon name="bell" size={22} color={Colors.patFontColor} />
      </View>
      <TouchableHighlight
        activeOpacity={1}
        underlayColor={Colors.white}
        style={styles.bell}
        onPress={goSigned}>
        <PatIcon
          name={isLogin ? 'signed' : 'no-sign-in'}
          size={22}
          color={Colors.patFontColor}
        />
      </TouchableHighlight>
      <View style={styles.free_ship}>
        <Text style={styles.title}>{t('free_ship', {price: 100})}</Text>
      </View>
      <TouchableHighlight
        activeOpacity={1}
        underlayColor={Colors.white}
        onPress={goCart}>
        <View style={styles.cart}>
          <PatIcon name="shopping-bag" size={22} color={Colors.patFontColor} />
          {count > 0 ? <Text style={styles.count}>{count}</Text> : null}
        </View>
      </TouchableHighlight>
    </View>
  );
};

const styles = StyleSheet.create({
  homeTop: {
    paddingTop: HEIGHT,
    height: 40 + HEIGHT,
    width: '100%',
    // backgroundColor: '#f40',
    position: 'relative',
    display: 'flex',
    justifyContent: 'flex-start',
    alignItems: 'center',
    flexDirection: 'row',
    overflow: 'visible',
  },
  bell: {
    width: 30,
    marginLeft: 10,
  },
  free_ship: {
    width: windowWidth - 130,
    marginRight: 10,
    height: 25,
    display: 'flex',
    justifyContent: 'center',
    alignItems: 'center',
  },
  title: {
    textAlign: 'center',
    lineHeight: 26,
    color: Colors.patFontColor,
  },
  cart: {
    width: 30,
    height: 25,
    marginRight: 10,
    display: 'flex',
    justifyContent: 'center',
    alignItems: 'center',
    position: 'relative',
  },
  count: {
    position: 'absolute',
    width: 18,
    height: 18,
    fontSize: 12,
    backgroundColor: Colors.patFontRed,
    color: Colors.white,
    borderRadius: 50,
    top: -5,
    right: -5,
    textAlign: 'center',
  },
});

export default memo(HomeTop);
